using Microsoft.EntityFrameworkCore;
using PandaWebAPI.Interfaces;
using PandaWebAPI.Models.ef;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PandaWebAPI.Services
{
    public class CartItemService : ICartItemService
    {
        private readonly PandaSystemContext _context;

        public CartItemService(PandaSystemContext context)
        {
            _context = context;
        }

        public async Task<IEnumerable<CartItem>> GetAllCartItemsAsync()
        {
            return await _context.CartItems
                .Include(c => c.Product)
                .Include(c => c.Cart).ThenInclude(c => c.User)
                .ToListAsync();
        }

        public async Task<CartItem?> GetCartItemByIdAsync(Guid id)
        {
            return await _context.CartItems
                .Include(c => c.Product)
                .Include(c => c.Cart).ThenInclude(c => c.User)
                .FirstOrDefaultAsync(c => c.CartItemId == id);
        }

        public async Task<IEnumerable<CartItem>> GetCartItemsByUserIdAsync(Guid userId)
        {
            return await _context.CartItems
                .Include(c => c.Product)
                .Include(c => c.Cart)
                .Where(c => c.Cart.UserId == userId)
                .ToListAsync();
        }

        public async Task<CartItem> CreateCartItemAsync(CartItem cartItem)
        {
            // 确保 CartItemId 是新生成的
            cartItem.CartItemId = Guid.NewGuid();
            cartItem.AddedAt = DateTime.UtcNow;
            
            await _context.CartItems.AddAsync(cartItem);
            await _context.SaveChangesAsync();
            
            return cartItem;
        }

        public async Task<CartItem> UpdateCartItemAsync(CartItem cartItem)
        {
            var existingCartItem = await _context.CartItems.FindAsync(cartItem.CartItemId);
            if (existingCartItem == null)
                throw new KeyNotFoundException($"CartItem with ID {cartItem.CartItemId} not found.");

            existingCartItem.Quantity = cartItem.Quantity;
            existingCartItem.AddedAt = DateTime.UtcNow; // 更新添加时间

            await _context.SaveChangesAsync();
            return existingCartItem;
        }

        public async Task<bool> DeleteCartItemAsync(Guid id)
        {
            var cartItem = await _context.CartItems.FindAsync(id);
            if (cartItem == null)
                return false;

            _context.CartItems.Remove(cartItem);
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<bool> ClearUserCartAsync(Guid userId)
        {
            // 先获取用户的购物车ID
            var cart = await _context.ShoppingCarts
                .FirstOrDefaultAsync(c => c.UserId == userId);
                
            if (cart == null)
                return false;

            var cartItems = await _context.CartItems
                .Where(c => c.CartId == cart.CartId)
                .ToListAsync();

            if (!cartItems.Any())
                return false;

            _context.CartItems.RemoveRange(cartItems);
            await _context.SaveChangesAsync();
            return true;
        }
    }
}